package com.zhang.controller;

import com.github.pagehelper.PageInfo;
import com.zhang.domain.Department;
import com.zhang.domain.DepartmentVO;
import com.zhang.query.DepartmentQuery;
import com.zhang.service.Impl.DepartmentServiceImpl;
import com.zhang.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zh
 * @create 2022-02-21 14:09
 */
@RestController
@RequestMapping("department")
@Api(value = "部门接口")
public class DepartmentController {
    @Resource
    private DepartmentServiceImpl service;
    @PutMapping
    @ApiOperation(value = "添加或修改接口", notes = "传入实体类中有id则执行修改，没有则执行添加")
    private AjaxResult addOrUpdate(@RequestBody Department d){
        DepartmentVO dv= new DepartmentVO();
        Long empId = service.queryEmpId(d.getManagerName());
        Long deptId = service.queryDeptId(d.getParentName());
        dv.setManagerId(empId);
        dv.setParentId(deptId);
        dv.setId(d.getId());
        dv.setSn(d.getSn());
        dv.setName(d.getName());
        dv.setDirPath(d.getDirPath());
        dv.setState(d.getState());
        if(dv.getId()!=null){
            try {
                service.update(dv);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false,"修改失败");
            }
        }else {
            try {
                service.add(dv);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false,"添加失败");
            }
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口", notes = "参数为id,类型是long类型")
    private AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败");
        }
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个部门信息接口", notes = "参数为id,类型是long类型")
    private AjaxResult getOne(@PathVariable("id") Long id){
        try {
            Department department = service.selectById(id);
            return AjaxResult.me(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @GetMapping
    @ApiOperation(value = "查询所有部门部门信息接口", notes = "无参")
    private AjaxResult getAll(){
        try {
            List<Department> departments = service.selectAll();
            return AjaxResult.me(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @PostMapping
    @ApiOperation(value = "分页查询接口", notes = "参数为当前页，和分页的条数")
    private AjaxResult getAllData(@RequestBody DepartmentQuery query){
        try {
            PageInfo<Department> pageInfo = service.selectData(query);
            return AjaxResult.me(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @DeleteMapping("/deleteAll")
    private AjaxResult deleteAll(@RequestBody Long[] id){
            try {
                service.deleteAll(id);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false,"修改失败");
            }
    }
    @GetMapping("/selAllMan")
    private AjaxResult selAllMan(){
        try {
            List<String> strings = service.selAllMan();
            return AjaxResult.me(strings);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }

}
